import Vue from 'vue';
import App from './app';
import util from './util';
import store from './store';
import qs from 'qs';
import request from 'superagent';
import { Notify, Toast, Form, Field, Button, Dialog, Uploader } from 'vant';
Vue.use(Notify);
Vue.use(Toast);
Vue.use(Form);
Vue.use(Field);
Vue.use(Button);
Vue.use(Uploader);
Vue.use(Dialog);

var $fly = require("flyio");
// 支持IE 浏览器
import 'babel-polyfill';
// 数据存储
import db from './db';
// 路由配置
import router from './router';
Vue.prototype.$db = db;
Vue.prototype.$util = util;
function getAndLoad(name, url, params, callback, errback) {
  if (params) {
    if (url.includes('?')) {
      url += '&' + qs.stringify(params);
    } else {
      url += '?' + qs.stringify(params);
    }
  }
  $fly.get(url).then(function (res) {
    if (res.data) {
      callback && callback(res.data);
    }
  })
  .catch(function () {
    errback && errback();
  });
}
function flypost (name, url, params, callback, errback) {
  request
  .post(url)
  .type('form')
  .send(params)
  .then(({body}) => {
    callback(body);
  })
  .catch(msg => {
    errback && errback();
  });
}
Vue.prototype.$fly = getAndLoad;
Vue.prototype.$flyP = flypost;

/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app")